<?php
declare (strict_types = 1);

namespace app\community\controller;

use think\Request;
use app\community\model\Community as comm;
use app\community\model\Leavings;

class Community
{
    public function communityList()
    {
        $data = comm::paginate(3);
        return json(['code'=>200,'msg'=>'查询成功','data'=>$data]);
    }

    public function communityLike()
    {
        $id = input('id');
        if(!empty($id)){
            return json(['code'=>4000,'msg'=>'参数有误','data'=>'']);
        }
        $like = input('like');
        $likes = $like+1;
        $data = comm::update(['id'=>$id,'like'=>$likes]);
        return json(['code'=>200,'msg'=>'点赞成功','data'=>$data]);
    }

    public function communityLeaving()
    {
        $user_id = input('user_id');
        $community_id = input('community_id');
        $leaving = input('leaving');
        $data = Leavings::create([
            'user_id'=>$user_id,
            'community_id'=>$community_id,
            'leaving'=>$leaving,
        ]);
        return json(['code'=>200,'msg'=>'留言成功','data'=>$data]);
    }

    public function communityAdd()
    {
        $user_id = input('user_id');
        $title = input('title');
        $community_img = input('community_img');
        $data = comm::create([
            'user_id'=>$user_id,
            'title'=>$title,
            'community_img'=>$community_img,
        ]);
        return json(['code'=>200,'msg'=>'发布成功','data'=>$data]);
    }
}
